<template>
  <div :style="'width:' + width">
    <el-form :label-width="labelWidth" label-position="left" :disabled="disabled">
      <el-form-item :label="name" :required="required">
        <el-checkbox-group v-model="data.values" @change="change">
          <el-checkbox v-for="(opt, index) in options" :key="index" :label="opt.Text">
            {{ opt.Text }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ['value', 'disabled', 'props', 'required', 'isBusiness'],
  data: function () {
    return {
      data: {
        values: [],
        displayName: '',
      },
    }
  },
  computed: {
    myprops() {
      return this.props ? this.props : []
    },
    labelWidth() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'labelWidth')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '140px'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '140px'
        }
      }
      return '140px'
    },
    width() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'width')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '100%'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '100%'
        }
      }
      return '100%'
    },
    name() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'name')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? val.BFFP_Value : '多选'
        } else {
          return val.FFLP_Value ? val.FFLP_Value : '多选'
        }
      }
      return '多选'
    },
    options() {
      let val = this.myprops.find((x) => x.FLPR_Code == 'options')
      if (val) {
        if (this.isBusiness) {
          return val.BFFP_Value ? JSON.parse(val.BFFP_Value) : []
        } else {
          return val.FFLP_Value ? JSON.parse(val.FFLP_Value) : []
        }
      }
      return []
    },
  },
  methods: {
    change() {
      this.data.displayName = JSON.stringify(this.data.values)
      this.$emit('input', JSON.stringify(this.data))
      this.$emit('display', this.data.displayName)
    },
  },
  mounted: function () {
    if (this.value && Object.keys(this.value).length) {
      this.data = JSON.parse(this.value)
    }
  },
}
</script>
<style scoped>
.el-form-item {
  margin: 10px !important;
}
</style>